package com.turing.common.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

import com.documents4j.api.DocumentType;
import com.documents4j.api.IConverter;
import com.documents4j.job.LocalConverter;

public class GetTypeByHead {

    private final static Map<String, String> FILE_TYPE_MAP = new HashMap<>();

    private GetTypeByHead() {
    }

    static {
        getAllFileType();  //初始化文件类型信息
    }

    private static void getAllFileType() {
        FILE_TYPE_MAP.put("ffd8ffe000104a464946", "jpg");
        FILE_TYPE_MAP.put("504b03040a0000000000", "docx");
        FILE_TYPE_MAP.put("255044462d312e340a25", "pdf");
        FILE_TYPE_MAP.put("255044462d312e360d25", "pdf");
        FILE_TYPE_MAP.put("504b0304140000000000", "zip");
        FILE_TYPE_MAP.put("377abcaf271c00042f9a", "7z");
        FILE_TYPE_MAP.put("d0cf11e0a1b11ae10000", "doc");
    }

    /**
     * 得到上传文件的文件头
     */
    private static String bytesToHexString(byte[] src) {
        StringBuilder stringBuilder = new StringBuilder();
        if (null == src || src.length <= 0) {
            return null;
        }
        for (byte b : src) {
            int v = b & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
        }
        return stringBuilder.toString();
    }

    /**
     * 获取文件类型
     */
    public static String getFileType(InputStream fis) {
        String res = null;
        try {
            @SuppressWarnings("resource")
            byte[] b = new byte[10];
            fis.read(b, 0, b.length);
            String fileCode = bytesToHexString(b);
            System.out.println("fileCode:" + fileCode);
            for (String key : FILE_TYPE_MAP.keySet()) {
                assert fileCode != null;
                if (key.toLowerCase().startsWith(fileCode.toLowerCase()) || fileCode.toLowerCase().startsWith(key.toLowerCase())) {
                    res = FILE_TYPE_MAP.get(key);
                    break;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return res;
    }

    public static void main(String[] args) {

        File inputWord = new File("/Users/luoshiwei/Downloads/创金合信华晟1号单一资产管理计划资产管理合同.doc");
        File outputFile = new File("/Users/luoshiwei/Desktop/tmp/kk.pdf");
        try {
            InputStream docxInputStream = new FileInputStream(inputWord);
            OutputStream outputStream = new FileOutputStream(outputFile);
            IConverter converter = LocalConverter.builder().build();
            converter.convert(docxInputStream).as(DocumentType.DOCX).to(outputStream).as(DocumentType.PDF).execute();
            outputStream.close();
            System.out.println("success");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}